const router = require('koa-router')()
const models = require('../models')
const moment = require('moment')
const Sequelize = require('sequelize');
const Op = Sequelize.Op;

router.post('/history_list', async ctx => {
    let page = ctx.request.body.page
    let pageshow = ctx.request.body.pageshow
    let fitment = await models.fitment.findAndCountAll({
        where: {
            del: 1
        },
        order: [
            ['created_at', 'DESC'],
        ],
        include: {
            model: models.user,
            attributes: ['username'],
        },
        limit: pageshow,
        offset: (page - 1)*pageshow
    })

    ctx.body = {
        code: 200,
        data: {
            list: fitment.rows.map(item => {
                let obj = JSON.parse(JSON.stringify(item))
                return {
                    ...obj,
                    created_at: moment(obj.created_at*1000).format('YYYY-MM-DD HH:mm:ss')
                }
            }),
            count: fitment.count
        },
        msg: '操作成功'
    }
})

router.post('/get_fitment', async ctx => {
    let type = ctx.request.body.type
    let zx_id = ctx.request.body.zx_id
    let user_id = ctx.request.body.user_id
    if (type == 'huifu') {
        let fitment = await models.fitment.findOne({
            where: {
                zx_id
            }
        })
        await fitment.set({
            user_id,
            created_at: Date.parse(new Date())/1000
        }).save().then(res => {
            return ctx.body = {
                code: 200,
                data: res,
                msg: '恢复成功'
            }
        }).catch(err => {
            return ctx.body = {
                code: 0,
                msg: '恢复失败'
            }
        })
    } else if (type == 'del') {
        let fitment = await models.fitment.findOne({
            where: {
                zx_id
            }
        })
        await fitment.set({
            user_id,
            del: 0,
            created_at: Date.parse(new Date())/1000
        }).save().then(res => {
            return ctx.body = {
                code: 200,
                data: res,
                msg: '删除成功'
            }
        }).catch(err => {
            return ctx.body = {
                code: 0,
                msg: '删除失败'
            }
        })
    } else {
        let fitment = await models.fitment.findAll({
            where: {
                del: 1
            },
            order: [
                ['created_at', 'DESC'],
            ],
            attributes: ['text', 'zx_id'],
            limit: 1
        })
        return ctx.body = {
            code: 200,
            data: {
                fitment
            },
            msg: '操作成功'
        }
    }
})

router.post('/set_fitment', async ctx => {
    let text = ctx.request.body.text
    let user_id = ctx.request.body.user_id
    let data = await models.fitment.create({
        user_id,
        text,
        created_at: Date.parse(new Date())/1000
    })
    ctx.body = {
        code: 200,
        data,
        msg: '操作成功'
    }
})


module.exports = router